package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.AlertDataPageQueryDto;
import com.zzyl.entity.AlertData;
import com.zzyl.mapper.AlertDataMapper;
import com.zzyl.service.AlertDataService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.AlertDataVo;
import org.apache.logging.log4j.spi.ObjectThreadContextMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class AlertDataServiceImpl implements AlertDataService {

       @Autowired
       private AlertDataMapper alertDataMapper;


       @Override
       public PageResponse<AlertDataVo> pageQuery(AlertDataPageQueryDto alertDataPageQueryDto) {
              PageHelper.startPage(alertDataPageQueryDto.getPageNum(), alertDataPageQueryDto.getPageSize());
               Long userId = UserThreadLocal.getMgtUserId();
               if(alertDataPageQueryDto.getStartTime()!=null &&alertDataPageQueryDto.getEndTime()!=null){
                   LocalDateTime start = LocalDateTimeUtil.of(alertDataPageQueryDto.getStartTime());
                   LocalDateTime end = LocalDateTimeUtil.of(alertDataPageQueryDto.getEndTime());
                   Page<AlertData> page=alertDataMapper.pageQuery(alertDataPageQueryDto.getDeviceName(),
                           alertDataPageQueryDto.getStatus(),start,end,userId);
                   PageResponse<AlertDataVo> of = PageResponse.of(page, AlertDataVo.class);
                   return of;
               }else{
                   Page<AlertData> page=alertDataMapper.pageQuery(alertDataPageQueryDto.getDeviceName(),
                           alertDataPageQueryDto.getStatus(),null,null,userId);
                   PageResponse<AlertDataVo> of = PageResponse.of(page, AlertDataVo.class);
                   return of;
               }

       }
}
